<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Car Control</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .control-buttons {
            margin: 20px 0;
            height: 50;
            width: 40;
        }
        .response {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center my-4">Car Control</h1>
        <div class="row">
            <div class="col text-center control-buttons">
                <button class="btn btn-primary" onclick="sendCommand('forward')">前进</button>
            </div>
        </div>
        <div class="row">
            <div class="col text-center control-buttons">
                <button class="btn btn-secondary" onclick="sendCommand('left')">左转</button>
                <button class="btn btn-danger" id="startStopButton" onclick="toggleStartStop()">开始</button>
                <button class="btn btn-secondary" onclick="sendCommand('right')">右转</button>
            </div>
        </div>
        <div class="row">
            <div class="col text-center control-buttons">
                <button class="btn btn-primary" onclick="sendCommand('backward')">后退</button>
            </div>
        </div>
        <div class="row">
            <div class="col text-center control-buttons">
                <button class="btn btn-success" onclick="sendCommand('accelerate')">加速</button>
                <button class="btn btn-warning" onclick="sendCommand('decelerate')">减速</button>
            </div>
        </div>
        <div class="row response">
            <div class="col text-center">
                <p id="response" class="alert alert-info"></p>
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap JS和依赖 -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <script>
        let isStarted = false;

        function sendCommand(command) {
            fetch('/send', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ message: command })
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('response').innerText = data.response;
            })
            .catch(error => console.error('Error:', error));
        }

        function toggleStartStop() {
            isStarted = !isStarted;
            const button = document.getElementById('startStopButton');
            if (isStarted) {
                button.innerText = '停止';
                button.classList.remove('btn-danger');
                button.classList.add('btn-success');
                sendCommand('start');
            } else {
                button.innerText = '开始';
                button.classList.remove('btn-success');
                button.classList.add('btn-danger');
                sendCommand('stop');
            }
        }
    </script>
</body>
</html>
